package org.jeecg.modules.basic.outstock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.basic.outstock.entity.LwgyOutStock;
import org.jeecg.modules.basic.outstock.entity.LwgyOutStocks;
import org.jeecg.modules.basic.outstock.mapper.LwgyOutStockMapper;
import org.jeecg.modules.basic.outstock.service.ILwgyOutStockService;
import org.jeecg.modules.basic.outstocktype.entity.LwgyOutStockType;
import org.jeecg.modules.basic.outstocktype.mapper.LwgyOutStocktypeMapper;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanGoods;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplication;
import org.jeecg.modules.basic.plan.mapper.LwgyMaterialPlanGoodsMapper;
import org.jeecg.modules.basic.plan.mapper.LwgyMaterialPlanapplicationsMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 出库管理表
 * @Author: jeecg-boot
 * @Date:   2021-04-02
 * @Version: V1.0
 */
@Service
public class LwgyOutStockServiceImpl extends ServiceImpl<LwgyOutStockMapper, LwgyOutStock> implements ILwgyOutStockService {

    @Resource
    private LwgyOutStockMapper lwgyOutStockMapper;

    @Resource
    private LwgyOutStocktypeMapper  lwgyOutStocktypeMapper;

    @Override
    public void saveMain(LwgyOutStock lwgyOutStock, List<LwgyOutStockType> lwgyOutStockTypeList) {
        lwgyOutStockMapper.insert(lwgyOutStock);
        if(lwgyOutStockTypeList!=null && lwgyOutStockTypeList.size()>0) {
            for(LwgyOutStockType entity:lwgyOutStockTypeList) {
                //外键设置
                entity.setApplyId(lwgyOutStock.getId());
                lwgyOutStocktypeMapper.insert(entity);
            }
        }
    }

    @Override
    public void updateMain(LwgyOutStock lwgyOutStock, List<LwgyOutStockType> lwgyOutStockTypeList) {
        lwgyOutStockMapper.updateById(lwgyOutStock);

        //1.先删除子表数据
        lwgyOutStocktypeMapper.deleteByMainId(lwgyOutStock.getId());

        //2.子表数据重新插入
        if(lwgyOutStockTypeList!=null && lwgyOutStockTypeList.size()>0) {
            for(LwgyOutStockType entity:lwgyOutStockTypeList) {
                //外键设置
                entity.setApplyId(lwgyOutStock.getId());
                lwgyOutStocktypeMapper.insert(entity);
            }
        }
    }

    @Override
    public void delMain(String id) {
        lwgyOutStocktypeMapper.deleteByMainId(id);
        lwgyOutStockMapper.deleteById(id);
    }

    @Override
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            lwgyOutStocktypeMapper.deleteByMainId(id.toString());
            lwgyOutStockMapper.deleteById(id);
        }
    }






    @Override
    public IPage<LwgyOutStocks> queryPageCustomer(Page<LwgyOutStocks> page, QueryWrapper<LwgyOutStocks> queryWrapper, String id) {
        return page.setRecords(lwgyOutStockMapper.queryPageListCustoms(page,queryWrapper,id));
    }

    @Override
    public void changeState(String id, Double progress) {
        lwgyOutStockMapper.changeState(id,progress);
    }

    @Override
    public List<LwgyOutStocks> queryPageExportListCustom(LwgyOutStocks lwgyOutStocks) {
        return lwgyOutStockMapper.queryPageExportListCustom(lwgyOutStocks);
    }
}
